// pages/control/control.js
var util = require("../../utils/util");
var util_md5 = require("../../utils/md5");
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    playStatus: true, //图片是否开启旋转
    singerImg: "", //歌手图片和背景图
    nowSongName: "", //
    nowSingerName: "", //
    xvolumeScroll: "", //音量条进度
    xvolumeTypeScroll: "", //音调进度
    volumeNumber: "", //设置的音量
    volumeTypeNumber: "",
    show: false, //调音弹框是否显示
    token: "", //用户token
    websocket_object: {},
    timeout: 50000,
    timeoutObj: null,
  },
  // 播放暂停
  playMusic() {
    var that = this;
    let params = {
      val: "",
    };
    util.songpostToken(
      "/xcstopplay.sendws",
      params,
      app.globalData.token,
      function (data) {
        console.log(data, "播放暂停");
      }
    );
    this.setData({
      playStatus: !this.data.playStatus,
    });
    console.log("播放暂停");
  },
  //  原唱/伴唱
  singerType() {
    var that = this;
    let params = {
      val: "",
    };
    console.log("原唱、伴唱");
    util.songpostToken(
      "/song_list/tune.sendws",
      params,
      app.globalData.token,
      function (data) {
        console.log(data, "原唱、伴唱");
      }
    );
  },
  //重唱
  reSong() {
    var that = this;
    let params = {
      val: "",
    };
    console.log("重唱");
    util.songpostToken(
      "/song_list/replay.sendws",
      params,
      app.globalData.token,
      function (data) {
        console.log(data, "重唱");
      }
    );
  },
  // 切歌
  nextSong() {
    var that = this;
    let params = {
      val: "",
    };
    console.log("重唱");
    util.songpostToken(
      "/xcnextr.sendws",
      params,
      app.globalData.token,
      function (data) {
        console.log(data, "重唱");
      }
    );
  },
  // 音量调节change事件
  volumeScroll(e) {
    var that = this;
    if (that.data.volumeNumber == Math.round((e.detail.x / 182) * 100)) {
    } else {
      that.setData({
        volumeNumber: Math.round((e.detail.x / 182) * 100),
      });
      // 100 % 是182px;
    }
  },
  // 音调调节change事件
  volumeTypeScroll(e) {
    var that = this;
    // console.log(e.detail);
    that.downOrUp(e.detail.x);
  },
  // 音量拖动停止事件
  touchmoveend() {
    var that = this;
    that.volPost();
  },
  // 音调拖动停止事件
  typetouchmoveend(e) {
    var that = this;
    console.log(that.data.volumeTypeNumber);
    that.volTypePost();
  },

  // 音量发送请求
  volPost() {
    var that = this;
    let params = {
      val: that.data.volumeNumber,
    };
    console.log(that.data.volumeNumber);
    util.songpostToken(
      "/volume.sendws",
      params,
      app.globalData.token,
      function (data) {
        console.log(data, "音量");
      }
    );
  },
  // 音调调节发送请求
  volTypePost() {
    var that = this;
    let params = {
      val: that.data.volumeTypeNumber,
    };
    util.songpostToken(
      "/mic/volume.sendws",
      params,
      app.globalData.token,
      function (data) {
        console.log(data, "音调");
      }
    );
  },
  // 音调数值计算
  downOrUp(number) {
    var that = this;
    let number1 = Math.round((number / 182) * 100);
    let number2 = Math.ceil(number1 / 4);
    //  console.log(number2,'整除')
    if (number2 == 13) {
      if (that.data.volumeTypeNumber == 0) {
      } else {
        that.setData({
          volumeTypeNumber: 0,
        });
        console.log("0");
      }
    } else if (number2 < 13 && number2 !== 0) {
      if (number2 - 13 == that.data.volumeTypeNumber) {
      } else {
        that.setData({
          volumeTypeNumber: number2 - 13,
        });
        console.log(number2 - 13);
      }
    } else if (number2 == 0) {
      if (that.data.volumeTypeNumber == -12) {
      } else {
        that.setData({
          volumeTypeNumber: -12,
        });
        console.log("-12");
      }
    } else {
      if (that.data.volumeTypeNumber == number2 - 13) {
      } else {
        that.setData({
          volumeTypeNumber: number2 - 13,
        });
        console.log(number2 - 13);
      }
    }
  },
  reverse(e) {
    var that = this;
    let itemX = 182 / 100;
    that.setData({
      xvolumeScroll: e * itemX,
    });
  },
  reverse_val(e) {
    var that = this;
    console.log(e);
    // let e=-10
    let itemX = 91 / 12;
    let x_sroll = "";
    if (Number(e) == -12) {
      x_sroll = 0;
    } else if (Number(e) == 0) {
      x_sroll = 91;
    } else if (Number(e) < 0) {
      console.log("小于0");
      x_sroll = (12 - Math.abs(e)) * itemX;
    } else if (Number(e) > 0) {
      x_sroll = e * itemX + 91;
    }
    console.log(x_sroll);
    that.setData({
      xvolumeTypeScroll: x_sroll,
    });
  },

  // 打开调音面板
  showAction() {
    console.log("111");
    this.setData({ show: true });
  },
  // 关闭调音面板
  onCancel() {
    console.log("222");
    this.setData({ show: false });
  },

  // 已点列表
  gotoyidianList: function (params) {
    wx.navigateTo({
      url: "../alreadyList/alreadyList",
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var token = wx.getStorageSync("token");
    console.log("app", app);
    that.setData({
      token,
      singerImg: app.globalData.singerImg, //歌手图片和背景图
      nowSongName: app.globalData.nowSongName, //
      nowSingerName: app.globalData.nowSingerName, //
      xvolumeScroll: that.reverse(app.globalData.volume_val), //音量条进度
      xvolumeTypeScroll: that.reverse_val(app.globalData.mic_volume_val), //音调进度
      volumeNumber: app.globalData.volume_val,
      volumeTypeNumber: app.globalData.mic_volume_val,
    });
    that.websocket_connect()
  },
  reset: function () {
    var that = this;
    // console.log('重置定时器心跳')
    clearTimeout(that.data.timeoutObj);
    that.start();
  },
  start: function () {
    var that = this;
    // console.log('定时器开始')
    that.data.timeoutObj = setTimeout(function () {
      that.data.websocket_object.send({
        data: "ping",
        success: function (res) {
          // console.log("心跳发送成功", res);
        },
      });
      // ws.send("HeartBeat");
    }, that.data.timeout);
  },
  websocket_connect() {
    var that=this
    var u_id = app.globalData.uid;
    var u_timestamp = new Date().getTime();
    console.log("时间戳1", u_timestamp);
    var u_verify =
      app.globalData.md5secret + "timestamp=" + u_timestamp + "&uid=" + u_id;
    console.log("未加密的签名1", u_verify);
    // 生成签名
    var signature = util_md5.hexMD5(u_verify);
    console.log("加密后的签名1", signature);
    that.data.websocket_object = wx.connectSocket({
      url:
        util.socketapi +
        "/connection.ws?uid=" +
        u_id +
        "&timestamp=" +
        u_timestamp +
        "&signature=" +
        signature,
      header: {
        "content-type": "application/x-www-form-urlencoded",
        Authorization: "Bearer " + that.data.token,
      },
      success: function (res) {
        console.log("连接成功1", res);
      },
    });
    that.data.websocket_object.onMessage(function (res) {
      that.reset()
      // console.log("接收1", JSON.parse(res.data));
      let receive_data = JSON.parse(res.data);
      let event_type = receive_data.data.event;
      if (event_type == "song_play") {
        that.setData({
          singerImg: receive_data.data.song.singerImg,
          nowSongName: receive_data.data.song.songName,
          nowSingerName: receive_data.data.song.singerName,
        });
      } else if (event_type == "mic_volume") {
        that.setData({
          volumeTypeNumber: receive_data.data.val,
        });
        that.reverse_val(receive_data.data.val);
      } else if (event_type == "volume") {
        that.setData({
          volumeNumber: receive_data.data.val,
        });
        that.reverse(receive_data.data.val);
      }
    });
    that.data.websocket_object.onClose(function (res) {
      console.log("断开1", res);
      that.websocket_connect();
    });
    that.data.websocket_object.onError(function (res) {
      console.log("错误事件", res);
      that.websocket_connect();
    });
    that.data.websocket_object.onOpen(function (res) {
      console.log("打开1", res);
      that.start();
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that=-this
    // that.data.websocket_object.close()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
